<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Menu extends AdminController {
    
    public function __construct(){
        parent::__construct();
    }

    public function index( $id_menu_principal = null, $page = 0 ){
        if ($id_menu_principal == null) {
            $id_menu_principal = $this->getDefaultMenuPrincipal();    
        } 
        if (isNotBlank($id_menu_principal)){
            $findBy = array( 'menu_principal' => $id_menu_principal );
            $offset = 0;
            if( $page != 0 ) {
                $offset = ( $page - 1 ) * LIMITE_PAGINACAO_ADMIN;
            }
            $lista = $this->doctrine->em->getRepository( 'Menus' )->findBy(
                $findBy, array('ordem' => 'ASC'), LIMITE_PAGINACAO_ADMIN, $offset
            );
            $count = $this->countWithWhere('menus', array('id_menu_principal' => $id_menu_principal));
            $this->assign( 'lista', $lista );
            $this->assign( 'total', $count );
            $this->assign( 'id_menu_principal', $id_menu_principal );
        }
        $this->display( 'menu/index' );
    }
    
    public function editar($id = null) {
        $em = $this->doctrine->em;
        if (!empty($id) && $_SERVER['REQUEST_METHOD'] == 'GET') {
            $obj = $em->getRepository('Menus')->find($id);
            $_POST = $obj->asArray();
        }  else {
            $_POST['menu_principal'] = $this->session->flashdata('id_menu_principal');
            $_POST['ativo'] = true;
        }
        $this->display('menu/editar');
    }
    
    public function novo($id_menu_principal = null){
        $this->session->set_flashdata('id_menu_principal', $id_menu_principal);
        redirect('admin/menu/editar');
    }
    
    public function save( $id = null ){
        try {
            if ($this->validate()){
                $em = $this->doctrine->em;
                $entity = new Menus();
                if( !empty($id)) {
                    $entity = $em->getRepository( 'Menus' )->find( $id );
                }
                populateForm( $entity, $this->input->post() );
                $menu_principal = $em->getRepository('MenusPrincipais')->find($entity->get_menu_principal());
                $entity->set_menu_principal($menu_principal);
                $em->persist( $entity );
                $em->flush();
                $this->showSuccess();
                redirect('admin/menu/index/' . $menu_principal->get_id_menu_principal());
            } 
        } catch (Exception $ex) {
            $this->showErros($ex);
        }
        $this->display('menu/editar');
    }
    
    private function validate(){
        $this->load->helper(array('form', 'url'));
        $this->form_validation->set_rules('menu_principal', 'Menu Superior', 'required');
        $this->form_validation->set_rules('titulo', 'Título', 'required');
        $this->form_validation->set_rules('ordem', 'Ordem', 'required|greater_than[0]');
        return $this->form_validation->run();
    }
    
    private function setFlash($key, $value){
        $this->session->set_flashdata($key, $value);
    }
    
    private function updateOrder($ordem){
        if ($this->existsOrder($ordem)){
            $sql = "update menus set ordem = ordem + 1 where ordem >= $ordem";
            $stmt = $this->doctrine->em->getConnection()->prepare($sql);
            $stmt->execute();
        }
    }
    
    private function existsOrder($ordem){
        $sql = "select 1 from menus m where m.ordem = $ordem";
        $stmt = $this->doctrine->em->getConnection()->prepare($sql);
        $stmt->execute();
        $result = $stmt->fetchAll();
        return !empty($result);
    }
    
    public function ajax_getGridMenus(){
        $id_menu_principal = $this->input->post('idMenuPrincipal');
        $page = $this->input->post('page');
        
        $findBy = array('menu_principal' => $id_menu_principal);
        $offset = 0;
        if( $page != 0 ) {
            $offset = ( $page - 1 ) * LIMITE_PAGINACAO_ADMIN;
        }
        
        $menus = $this->doctrine->em->getRepository( 'Menus' )->findBy(
            $findBy, array('ordem' => 'ASC'), LIMITE_PAGINACAO_ADMIN, $offset
        );
        
        $data = array();
        $data['lista'] = $menus;
        $data['total'] = $this->count('Menus');
        echo $this->load->view('admin/menu/components/lista_menus', $data);
    }
    
    /////////////////////////////////////////////////
    
    private function getDefaultMenuPrincipal(){
        $sql = "select id_menu_principal from menus_principais order by id_menu_principal limit 0,1 ";
        $stmt = $this->doctrine->em->getConnection()->prepare($sql);
        $stmt->execute();
        return $stmt->fetchColumn(0);
        
    }
}
/* End of file home.php */
/* Location: ./application/controllers/admin/home.php */